<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='jslet-ui-ImportDialog'>/**
</span> * @class
 * 
 * Import dialog for specified Dataset object. Example:
 * 
 *     @example
 *     var dlg = new jslet.ui.ImportDialog(dsObj, {includeFields: ['fld1', 'fld2']});
 *     var dlg = new jslet.ui.ImportDialog('employee');
 * 
 * @param {jslet.data.Dataset | String} dataset (Required)Dataset object or dataset name.
 * @param {Object} options (Optional)Import options. 
 * @param {Boolean} options.hasSchemaSection Identify whether import schema section is visible or not. 
 * @param {String[]} options.includeFields Specify which fields need import. 
 * @param {String[]} options.excludeFields Specify which fields need not import. 
 */
jslet.ui.ImportDialog = function(dataset, options) {
	this._dataset = jslet.data.getDataset(dataset);
	this._importDataset = null;
	
	this._parsedData = null;
	
	this._dlgId = null;
	
	this._onCustomImport = null;
	this._onImporting = null;
	this._onImported = null;
	
	this._onQuerySchema = null;
	this._onSubmitSchema = null;
	
	this._schemaDsName = null;
	this._schemaLkDsName = null;
	
	this._colHeaders = null;
	
	this._progressBar = null;
	
	this._hasSchemaSection = false;
	
	this._includeFields = null;
	
	this._excludeFields = null;
	
	if(options) {
		this._hasSchemaSection = (options.hasSchemaSection? true: false);
		this._includeFields = options.includeFields;
		this._excludeFields = options.excludeFields;
	}
	this._initialize();
};

jslet.ui.ImportDialog.prototype = {
<span id='jslet-ui-ImportDialog-method-show'>	/**
</span>	 * Show import dialog
	 */
	show: function() {
		var Z = this;
		if(Z._hasSchemaSection) {
			Z._querySchema();
		}
		Z._refreshFields();
		var jqEl = jQuery('#' + this._dlgId);
		jqEl.find('#jltxtImportFile').val('');
		var owin = jqEl[0].jslet;
		owin.showModal();
		owin.setZIndex(999);
		return owin;
	},

<span id='jslet-ui-ImportDialog-event-onQuerySchema'>	/**
</span>	 * @event
	 * 
	 * Query import schema event handler. Example:
	 * 
	 *     @example
	 *     var importDialog = new jslet.ui.ImportDialog(importingDataset, {hasSchemaSection: true});
	 *     var querySchemaFn = function(callBackFn) {
	 * 	      var exportSchemaData = [{schema: '', fields: ['field1','field2']}];
	 * 	      callBackFn(exportSchemaData); //For asynchronous operation.
	 * 	     //return exportSchemaData; 	//For synchronous operation.
	 *     };
	 * 
	 *     importDialog.onQuerySchema(querySchemaFn);
	 *     
	 * @param {Function} onQuerySchema Query schema event handler.
	 * @param {Function} onQuerySchema.callBackFn Query schema callback function.
	 * @param {Function} onQuerySchema.callBackFn.exportSchemaData Query schema.
	 * 
	 * @return {this | Function}
	 */
	onQuerySchema: function(onQuerySchema) {
		if(onQuerySchema === undefined) {
			return this._onQuerySchema;
		}
		jslet.Checker.test('ImportDialog#onQuerySchema', onQuerySchema).isFunction();
		this._onQuerySchema = onQuerySchema;
		return this;
	},	
	
<span id='jslet-ui-ImportDialog-event-onSubmitSchema'>	/**
</span>	 * @event
	 * 
	 * Submit import schema event handler. Example:
	 * 
	 *     @example
	 *     var importDialog = new jslet.ui.ImportDialog(importingDataset, {hasSchemaSection: true});
	 *     var submitSchemaFn = function(action, changedRec) {
	 * 	     if(action == 'insert') {}
	 * 	     if(action == 'update') {}
	 * 	     if(action == 'delete') {}
	 *     };
	 * 
	 *     importDialog.onSubmitSchema(submitSchemaFn);
	 *     
	 * @param {Function | undefined} onSubmitSchema Submitting schema event handler.
	 * @param {String} onSubmitSchema.action Submit action, optional value: 'insert', 'update', 'delete'.
	 * @param {Object} onSubmitSchema.changedRecord Changed record.
	 * 
	 * @return {this | Function}
	 */
	onSubmitSchema: function(onSubmitSchema) {
		if(onSubmitSchema === undefined) {
			return this._onSubmitSchema;
		}
		jslet.Checker.test('ImportDialog#onSubmitSchema', onSubmitSchema).isFunction();
		this._onSubmitSchema = onSubmitSchema;
		return this;
	},
	
<span id='jslet-ui-ImportDialog-event-onCustomImport'>	/**
</span>	 * @event
	 * 
	 * Customized importing event. Example:
	 * 
	 *     @example
	 *     var importDialog = new jslet.ui.ImportDialog(importingDataset);
	 *     importDialog.onCustomImport(function(importingDataset, parsedData, fieldMaps) { });
	 *     
	 * @param {Function | undefined} onCustomImport Customized importing event.
	 * @param {jslet.data.Dataset} onCustomImport.importingDataset Importing dataset object.
	 * @param {Object[]} onCustomImport.parsedData Parsed data from excel file.
	 * @param {Object[]} onCustomImport.fieldMaps Field map to excel header.
	 * 
	 * @return {this | Function}
	 */
	onCustomImport: function(onCustomImport) {
		if(onCustomImport === undefined) {
			return this._onCustomImport;
		}
		jslet.Checker.test('ImportDialog#onCustomImport', onCustomImport).isFunction();
		this._onCustomImport = onCustomImport;
		return this;
	},
	
<span id='jslet-ui-ImportDialog-event-onImporting'>	/**
</span>	 * @event
	 * 
	 * Importing event. Example:
	 * 
	 *     @example
	 *     var importDialog = new jslet.ui.ImportDialog(importingDataset);
	 *     importDialog.onImporting(function(fieldName, excelColValue) { });
	 *     
	 * @param {Function | undefined} onImporting Importing event.
	 * @param {String} onImporting.fieldName The importing field name.
	 * @param {String} onImporting.excelColValue The mapping column value of excel file.
	 * 
	 * @return {this | Function}
	 */
	onImporting: function(onImporting) {
		if(onImporting === undefined) {
			return this._onImporting;
		}
		jslet.Checker.test('ImportDialog#onImporting', onImporting).isFunction();
		this._onImporting = onImporting;
		return this;
	},
	
<span id='jslet-ui-ImportDialog-event-onImported'>	/**
</span>	 * @event
	 * 
	 * Imported event. Example:
	 * 
	 *     @example
	 *     var importDialog = new jslet.ui.ImportDialog(importingDataset);
	 *     importDialog.imported(function(dataset) { });
	 *     
	 * @param {Function | undefined} onImported Imported event.
	 * @param {jslet.data.Dataset} onImported.dataset Importing dataset object.
	 * 
	 * @return {this | Function}
	 */
	onImported: function(onImported) {
		if(onImported === undefined) {
			return this._onImported;
		}
		jslet.Checker.test('ImportDialog#onImported', onImported).isFunction();
		this._onImported = onImported;
		return this;
	},
	
	_initialize: function() {
		var Z = this;
		var fldCfg = [
		              {name: 'schema', type: 'S', length: 100, label: 'Schema'},
		              {name: 'fieldMaps', type: 'S', length: 5000, label: 'fieldMaps', visible: false},
		              {name: 'colHeaders', type: 'S', length: 5000, label: 'fieldMaps', visible: false}
		              ];
		var schemaLKDs = new jslet.data.Dataset({name: 'schemaLKDs' + jslet.nextId(), fields: fldCfg, 
				keyField: 'schema', codeField: 'schema', nameField: 'schema', autoRefreshHostDataset: true, auditLogEnabled: false, logChanges: false});
		
		var importDialogLocale = jsletlocale.ImportDialog;
		Z._schemaLkDsName = schemaLKDs.name();
		fldCfg = [
		    {name: 'schema', type: 'S', length: 100, label: 'Schema', lookup: {dataset: schemaLKDs}}
        ];		
		Z._schemaDsName = 'schemaDs' + jslet.nextId();
		var schemaDs = new jslet.data.Dataset({name: Z._schemaDsName, fields: fldCfg}); 
		schemaDs.onFieldChanged(function(fldName, fldValue) {
			Z._doSchemaChanged(this, fldValue);
		});
		
		fldCfg = [
	    	      {name: 'colHeader', type: 'S', length: 100, label: 'Column Header', displayWidth: 16}
	    	    ];
		var exportLKDs = new jslet.data.Dataset({name: 'exportLKDs' + jslet.nextId(), fields: fldCfg, 
				keyField: 'colHeader', codeField: 'colHeader', nameField: 'colHeader', autoRefreshHostDataset: true});
		
		var expFldCfg = [
       	      {name: 'field', type: 'S', length: 100, label: 'Field Name', visible: false}, 
	   	      {name: 'label', type: 'S', length: 100, visible: false},
	   	      {name: 'displayLabel', type: 'S', length: 100, label: importDialogLocale.fieldLabel, displayWidth: 12, readOnly: true},
    	      {name: 'colHeader', type: 'S', length: 100, label: importDialogLocale.columnHeader, displayWidth: 12, editControl: 'DBSelect',
    	    	  lookup: {dataset: exportLKDs}},
    	      {name: 'required', type: 'B', length: 10, label: 'required', visible: false},
    	      {name: 'fixedValue', type: 'S', length: 50, label: importDialogLocale.fixedValue, displayWidth: 12}
    	    ];
    	Z._importDataset = new jslet.data.Dataset({name: 'importDs' + jslet.nextId(), fields: expFldCfg, keyField: 'schema', nameField: 'schema', isFireGlobalEvent: false});
		var opt = { type: 'window', caption: importDialogLocale.caption, isCenter: true, resizable: false, minimizable: false, maximizable: false, animation: 'fade'};
		var owin = jslet.ui.createControl(opt);
		var expHtml = '';
    	if(Z._hasSchemaSection) {
    		expHtml = 
	            '&lt;div class=&quot;input-group input-group-sm&quot; style=&quot;margin-bottom: 10px&quot;&gt;&lt;span class=&quot;input-group-addon&quot;&gt;' +
	            importDialogLocale.schemaName + 
	            '&lt;/span&gt;' + 
	            '&lt;select data-jslet=&quot;type:\'DBSelect\',dataset: \'' + schemaDs.name() + '\', field:\'schema\'&quot;&gt;&lt;/select&gt;' + 

	            '&lt;span class=&quot;input-group-btn&quot;&gt;&lt;button class=&quot;btn btn-default btn-sm&quot; name=&quot;jlbtnSaveAs&quot;&gt;' + 
	            importDialogLocale.saveAsSchema + 
	            '&lt;/button&gt;&lt;/span&gt;' +
	            '&lt;span class=&quot;input-group-btn&quot;&gt;&lt;button class=&quot;btn btn-default btn-sm&quot; name=&quot;jlbtnSave&quot;&gt;' + 
	            importDialogLocale.saveSchema + 
	            '&lt;/button&gt;&lt;/span&gt;' +
	            '&lt;span class=&quot;input-group-btn&quot;&gt;&lt;button class=&quot;btn btn-default btn-sm&quot; name=&quot;jlbtnDelete&quot;&gt;' + 
	            importDialogLocale.deleteSchema + 
	            '&lt;/button&gt;&lt;/span&gt;' +
	            '&lt;/div&gt;';
    	}

		var html = ['&lt;div class=&quot;form-horizontal jl-impdlg-content&quot;&gt;',
	            expHtml,
	            '&lt;div class=&quot;input-group input-group-sm&quot;&gt;&lt;span class=&quot;input-group-addon&quot;&gt;',
	            importDialogLocale.fileName,
	            '&lt;/span&gt;',
				'&lt;input name=&quot;jltxtImportFile&quot; title=&quot;*.xls|*.xlsx|*.xlsb|*.xlsm|*.ods&quot; class=&quot;form-control&quot; type=&quot;file&quot;&gt;&lt;/input&gt;',
				'&lt;/div&gt;',
				
	            '&lt;div class=&quot;col-sm-12 jl-impdlg-fieldmap&quot; style=&quot;padding: 0!important&quot;&gt;',
	            '&lt;div data-jslet=&quot;type:\'DBTable\', dataset: \'', 
	            Z._importDataset.name(), 
	            '\', editable: true, hasFilterDialog:false, disableHeadSort: true&quot;&gt;&lt;/div&gt;&lt;/div&gt;',

				'&lt;div class=&quot;col-sm-12&quot;&gt;&lt;/div&gt;',
	            '&lt;div class=&quot;input-group input-group-sm jl-impdlg-toolbar&quot; style=&quot;margin-bottom: 0;width:100%&quot;&gt;',
	            '&lt;div class=&quot;col-sm-9&quot;&gt;&lt;div name=&quot;jlProgressImport&quot; data-jslet=&quot;type:\'ProgressBar\', value: 50&quot; style=&quot;display:none;width:100%&quot;&gt;&lt;/div&gt;&lt;/div&gt;',
	            '&lt;div class=&quot;col-sm-3&quot;&gt;&lt;button name=&quot;jlbtnCancel&quot; class=&quot;btn btn-default btn-sm jl-impdlg-toolbutton&quot;&gt;',
	            importDialogLocale.cancel,
	            '&lt;/button&gt;&lt;button name=&quot;jlbtnImport&quot; class=&quot;btn btn-default btn-sm jl-impdlg-toolbutton&quot;&gt;',
	            importDialogLocale.importData,
	            '&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;',
	            '&lt;/div&gt;'];
		owin.setContent(html.join(''));
		owin.onClosed(function () {
			Z.destroy();
		});
		jslet.ui.install(owin.el);
		Z._dlgId = owin.el.id;
		var jqEl = jQuery(owin.el);
		Z._progressBar = jqEl.find('[name=&quot;jlProgressImport&quot;]')[0].jslet;

		jqEl.find('[name=&quot;jlbtnSaveAs&quot;]').click(function(event) {
			Z._saveAsSchema();
		});
		
		jqEl.find('[name=&quot;jlbtnSave&quot;]').click(function(event) {
			Z._saveSchema();
		});
		
		jqEl.find('[name=&quot;jlbtnDelete&quot;]').click(function(event) {
			Z._deleteSchema();
		});
		
		jqEl.find('[name=&quot;jltxtImportFile&quot;]').on('change', function(event) {
			var files = event.delegateTarget.files;
			if(files.length &gt; 0) {
				Z._readFile(files[0]);
			}
		});
		jqEl.find('[name=&quot;jlbtnImport&quot;]').click(function(event) {
			Z._importData();
		});
		
		jqEl.find('[name=&quot;jlbtnCancel&quot;]').click(function(event) {
			owin.close();
		});
	}, //End of '_initialize'
	
	_querySchema: function() {
		var Z = this,
			queryFn = this._onQuerySchema || jslet.global.importDialog.onQuerySchema;
		
		function innerRefresh(schemaData) {
			if(!schemaData) {
				return;
			}
			jslet.data.getDataset(Z._schemaLkDsName).records(schemaData);
		}
		
		if(!queryFn) {
			console.warn('Event handler: onQuerySchema NOT set, can not query export schema!');
			return;
		}
		var result = queryFn();
		if(result &amp;&amp; jslet.isPromise(result)) {
			result.done(function(schemaData) {
				innerRefresh(schemaData);
			});
		} else {
			innerRefresh(result);
		}
	},
	
	_submitSchema: function(action, changedRecord) {
		var Z = this;
		
		var actionFn = Z._onSubmitSchema || jslet.global.importDialog.onSubmitSchema;
		if(actionFn) {
			delete changedRecord._jl_;
			return actionFn(action, changedRecord);
		}
		return null;
	},
	
	_saveAsSchema: function() {
		var Z = this;
		var fieldMaps = Z._getFieldMaps();
		if(!fieldMaps) {
			return;
		}
		jslet.ui.prompt(jsletlocale.ExportDialog.inuputSchemaLabel, null, function(button, schemaName){
			if(button === 'ok' &amp;&amp; schemaName) {
				var dsSchema = jslet.data.getDataset(Z._schemaDsName);
				var dsSchemaLk = jslet.data.getDataset(Z._schemaLkDsName);
				var context = dsSchemaLk.startSilenceMove();
				var foundRecno = -1;
				var breakDown = false;
				try {
					if(dsSchemaLk.findByKey(schemaName)) {
		            	foundRecno = dsSchemaLk.recno();
					}
				} finally {
					dsSchemaLk.endSilenceMove(context);
				}
				if(foundRecno &gt;= 0) {
		            jslet.ui.confirm(jsletlocale.ImportDialog.existedSchema, null, function(button){
		            	if(button == 'cancel') {
		            		return;
		            	}
		            	dsSchemaLk.recno(foundRecno);
						dsSchemaLk.editRecord();
						dsSchemaLk.setFieldValue('schema', schemaName);
						var strMaps = JSON.stringify(fieldMaps);
						dsSchemaLk.setFieldValue('fieldMaps', strMaps);
						dsSchemaLk.setFieldValue('colHeaders', JSON.stringify(Z._getColHeaders()));
						dsSchemaLk.confirm();
						Z._isSaving = true;
						try {
							dsSchema.setFieldValue('schema', schemaName);
						} finally {
							Z._isSaving = false;
						}
						Z._submitSchema('insert', dsSchemaLk.getRecord());
		            });
				} else {
					dsSchemaLk.appendRecord();
					dsSchemaLk.setFieldValue('schema', schemaName);
					var strMaps = JSON.stringify(fieldMaps);
					dsSchemaLk.setFieldValue('fieldMaps', strMaps);
					dsSchemaLk.setFieldValue('colHeaders', JSON.stringify(Z._getColHeaders()));
					dsSchemaLk.confirm();
					Z._isSaving = true;
					try {
						dsSchema.setFieldValue('schema', schemaName);
					} finally {
						Z._isSaving = false;
					}
					Z._submitSchema('insert', dsSchemaLk.getRecord());
				}
			}
		});
	},
	
	_saveSchema: function() {
		var Z = this,
			dsSchemaLk = jslet.data.getDataset(Z._schemaLkDsName),
			dsSchema = jslet.data.getDataset(Z._schemaDsName);
		if(dsSchemaLk.recordCount() === 0 || !dsSchema.getFieldValue('schema')) {
			Z._saveAsSchema();
			return;
		}
		var fieldMaps = Z._getFieldMaps();
		if(!fieldMaps) {
			return;
		}
		dsSchemaLk.editRecord();
		var strMaps = JSON.stringify(fieldMaps);
		dsSchemaLk.setFieldValue('fieldMaps', fieldMaps);
		dsSchemaLk.setFieldValue('colHeaders', JSON.stringify(Z._getColHeaders()));
		
		dsSchemaLk.confirm();
		Z._submitSchema('update', dsSchemaLk.getRecord());
	},
	
	_deleteSchema: function() {
		var Z = this,
			dsSchemaLk = jslet.data.getDataset(Z._schemaLkDsName),
			dsSchema = jslet.data.getDataset(Z._schemaDsName);
		if(dsSchemaLk.recordCount() === 0 || !dsSchema.getFieldValue('schema')) {
			return;
		}
		var result = Z._submitSchema('delete', dsSchemaLk.getRecord());
		if(result &amp;&amp; jslet.isPromise(result)) {
			result.done(function() {
				dsSchemaLk.deleteRecord();
				dsSchema.setFieldValue('schema', null);
				dsSchema.confirm();
			});
		} else {
			dsSchemaLk.deleteRecord();
			dsSchema.setFieldValue('schema', null);
			dsSchema.confirm();
		}
	},
	
	_refreshFields: function() {
		var fieldRecords = [], fldObj, fldName, required, label,
			fields = this._dataset.getNormalFields(), 
			labels = {}, errLabels = null,
			includeFields = this._includeFields,
			excludeFields = this._excludeFields;
		for(var i = 0, len = fields.length; i &lt; len; i++) {
			fldObj = fields[i];
			fldName = fldObj.name();
			if(includeFields) {
				if(includeFields.indexOf(fldName) &lt; 0) {
					continue;
				}
			} else {
				if(!fldObj.visible()) {
					continue;
				}	
			}
			if(excludeFields &amp;&amp; excludeFields.indexOf(fldName) &gt;= 0) {
				continue;
			}
			required = fldObj.required();
			label = fldObj.label();
			if(labels[label]) {
				if(errLabels) {
					errLabels += ',' + errLabels;
				} else {
					errLabels = label;
				}
			} else {
				labels[label] = true;
			}
			fieldRecords.push({field: fldName, label: label, displayLabel: label + (required? '&lt;span class=&quot;jl-lbl-required&quot;&gt;*&lt;/span&gt;': ''), required: required});
		}
		if(errLabels) {
			jslet.showInfo(jslet.formatMessage(jsletlocale.ImportDialog.duplicateFields, [errLabels]));
		}
		this._importDataset.records(fieldRecords);
		this._importDataset.first();
		this._colHeaders = null;
	},

	_readFile: function(fileObj) {
		var Z = this,
			name = fileObj.name,
			suffix = name.substring(name.lastIndexOf('.') + 1) || '';
		suffix = suffix.toLowerCase();
		if(suffix != 'xls' &amp;&amp; suffix != 'xlsx' &amp;&amp; suffix != 'xlsb' &amp;&amp; suffix != 'xlsm' &amp;&amp; suffix != 'ods') {
			jslet.showError(jsletlocale.ImportDialog.notSupportFile);
			return;
		}
	    var	reader = new window.FileReader();
		reader.onload = function(e) {
			var fileContent,
				parsedResult = null;
			if(e) {
				fileContent = e.target.result;
			} else {
				fileContent = reader.content;
			}
			try {
				parsedResult = jslet.data.defaultXPorter.excelXPorter().importData(Z._dataset, fileContent);
			} catch(e) {
				console.error(e);
				jslet.showError(jsletlocale.ImportDialog.notSupportFile);
			}
			if(parsedResult) {
				Z._colHeaders = parsedResult.header;
				Z._addColumnHeader(Z._colHeaders);
				Z._parsedData = parsedResult.data;
			} else {
				jslet.showError(jsletlocale.ImportDialog.noData);
			}
		};
		reader.readAsBinaryString(fileObj);
	},
	
	_addColumnHeader: function(headers) {
		if(!headers) {
			return;
		}
		var fieldRecords = [], i, len;
		for(i = 0, len = headers.length; i &lt; len; i++) {
			fieldRecords.push({/*colNum: i,*/ colHeader: headers[i]});
		}
		this._importDataset.getField('colHeader').lookup().dataset().records(fieldRecords);
		this._mapFieldColumn();
	},
	
	_mapFieldColumn: function() {
		var headers = this._colHeaders;
		if(!headers) {
			return;
		}
		var records = this._importDataset.records(),
			label, rec, found = false;
		for(var i = 0, len = records.length; i &lt; len; i++) {
			rec = records[i];
			if(!rec.fixedValue) {
				label = rec.colHeader || rec.label;
				if(headers.indexOf(label) &gt;= 0) {
					rec.colHeader = label;
					found = true;
				} else {
					rec.colHeader = null;
				}
			} else {
				if(rec.colHeader) {
					rec.colHeader = null;
					found = true;
				}
			}
		}
		if(found) {
			this._importDataset.records(records);
		}
	},
	
	_doSchemaChanged: function(dsSchema, schemaName) {
		if(this._isSaving) {
			return;
		}
		var mapFlds = null,
			colHeaders, records,
			mapFldCnt = 0,
			dsSchemaLk = jslet.data.getDataset(this._schemaLkDsName);
		if(schemaName) {
			mapFlds = JSON.parse(dsSchema.getFieldValue('schema.fieldMaps'));
			mapFldCnt = mapFlds.length;
			
			var arrColHeaders = JSON.parse(dsSchema.getFieldValue('schema.colHeaders'));
			records = [];
			for(var k = 0, headerCnt = arrColHeaders.length; k &lt; headerCnt; k++) {
				records.push({colHeader: arrColHeaders[k]});
			}
			var dsColHeader = this._importDataset.getField('colHeader').lookup().dataset();
			dsColHeader.records(records);
		}
		records = this._importDataset.records();
		var rec, fldName, schColHeader, mapFld;
		for(var i = 0, len = records.length; i &lt; len; i++) {
			rec = records[i];
			fldName = rec.field;
			rec.colHeader = null;
			rec.fixedValue = null;
			schColHeader = null;
			if(mapFlds) {
				for(var j = 0; j &lt; mapFldCnt; j++) {
					mapFld = mapFlds[j];
					if(mapFld.field == fldName) {
						rec.colHeader = mapFld.colHeader;
						rec.fixedValue = mapFld.fixedValue;
						break;
					}
				}
			}
		}
		this._importDataset.records(records);
	},
	
	_getFieldMaps: function() {
		var records = this._importDataset.records(), 
			row, fieldMaps = [];
		for(var i = 0, len = records.length; i &lt; len; i++) {
			row = records[i];
			if(row.colHeader || row.fixedValue) {
				fieldMaps.push({field: row.field, colHeader: row.colHeader, fixedValue: row.fixedValue});
			} else if(row.required) {
				jslet.showInfo(jsletlocale.ImportDialog.noColHeader);
				return null;
			}
		}
		var fldCnt = fieldMaps.length;
		if(fldCnt === 0) {
			jslet.showInfo(jsletlocale.ImportDialog.noColHeader);
			return null;
		}
		return fieldMaps;
	},
	
	_getColHeaders: function() {
		var colHeaders = [],
			records = this._importDataset.getField('colHeader').lookup().dataset().records();
		for(var i = 0, len = records.length; i &lt; len; i++) {
			colHeaders.push(records[i].colHeader);
		}
		return colHeaders;
	},
	
	_importData: function() {
		var Z = this;
		if(!Z._parsedData || Z._parsedData.length === 0) {
			jslet.showInfo(jsletlocale.ImportDialog.noData);
			return;
		}
		var fieldMaps = Z._getFieldMaps();
		if(!fieldMaps) {
			return;
		}
		
		if(Z._onCustomImport) {
			var isSuccess = Z._onCustomImport(Z._dataset, Z._parsedData, fieldMaps);
			if(!isSuccess) {
				return;
			}
		} else {
			var fldMap, text, colHeader,
				masterDs = Z._dataset, 
				parsedData = Z._parsedData,
				textList = [], textRec, row,
				fldCnt = fieldMaps.length,
				importingFn = Z._onImporting;
			
			for(var i = 0, len = parsedData.length; i &lt; len; i++) {
				row = parsedData[i];
				textRec = {};
				for(var j = 0; j &lt; fldCnt; j++) {
					fldMap = fieldMaps[j];
					colHeader = fldMap.colHeader; 
					if(colHeader) {
						if(importingFn) {
							text = importingFn(fldMap.field, row[colHeader]);
						} else {
							text = row[colHeader];
						}
					} else {
						text = fldMap.fixedValue;
					}
					if(text) {
						textRec[fldMap.field] = text;
					}
				}
				textList.push(textRec);
			}
			var jqEl = jQuery('#' + Z._dlgId);
			jqEl.find('[name=&quot;jlProgressImport&quot;]').css('display', '');
			var dsObj = Z._dataset;
			var progressBarObj = Z._progressBar;
			dsObj.disableControls();
			new jslet.StepProcessor(textList.length, function(start, end, percent) {
				try {
					dsObj.importTextList(textList, start, end);
				} catch (e) {
					dsObj.enableControls();
					throw e;
				}
				progressBarObj.value(percent);
				if(percent === 100) {
					dsObj.enableControls();
					dsObj.first();
					Z._doImported();
				}
			}).run();
		}
		
		return;
	},
	
	_doImported: function() {
		var Z = this;
		if(Z._onImported) {
			Z._onImported(Z._dataset);
		}
		jslet('#' + Z._dlgId).close();
	},
	
	destroy: function() {
		var Z = this;
		Z._onImporting = null;
		Z._onImported = null;
		Z._onCustomImport = null;
		Z._progressBar = null;
		
		Z._dataset = null;
		var dsSchemaLk = jslet.data.getDataset(Z._schemaLkDsName);
		if(dsSchemaLk) {
			dsSchemaLk.destroy();
		}
		var dsSchema = jslet.data.getDataset(Z._schemaDsName);
		if(dsSchema) {
			dsSchema.destroy();
		}
		if(Z._importDataset) {
    		var lkds = Z._importDataset.getField('colHeader').lookup().dataset();
    		lkds.destroy();
    		this._importDataset.destroy();
    		this._importDataset = null;
		}
	}
};


</pre>
</body>
</html>
